package com.example.test.b2btest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class requestUtils {

    //参数
    public static final String DEF_CHATSET = "UTF-8";
    public static final int DEF_CONN_TIMEOUT = 30000;
    public static final int DEF_READ_TIMEOUT = 30000;
    public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";

    //配置您申请的KEY
    public static final String APPKEY = "*************************";

    public static String token = "";

    // http://192.168.2.19:9092
    // http://dev.yuedaowang.com:9091
    //http://192.168.2.190:9091
    //API具体方法

    public static final String localUrl = "http://192.168.2.32:9091";

    public static final String devUrl = "http://dev.yuedaowang.com:9091";

    public static void main(String[] args) {
       //b2bTest();


        login();
       // getPrice();
        createOrder();
        //cancelOrder();
        //payOrderNotify();
         // getOrderInfo();
        //getOrderList();
        //createReceipt();
        //getPartnerInfo();
        //afterSales();

    }


    public static void b2bTest() {
        String result = null;
        String url = "http://119.254.233.174/UmeCar/callback/YDupdateOrderStatus";//请求接口地址
        Map<String, Map<String, String>> params1 = new HashMap();//请求参数体
        Map<String, String> params = new HashMap<>();//请求参数

        params.put("orderNo", "YDX2018091415130508660379");
        params.put("statusNo", "3");
        params.put("operation", "YDCallBackFunction");

        params1.put("req",params);

        B2bTest b = new B2bTest();
        b.setOrderNo("YDX2018091415130508660379");
        b.setStatusNo("3");
        b.setOperation("YDCallBackFunction");

        try {
            result = net(url, params1, "get");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void passagerLogin() {
        String result = null;
        String url = devUrl+"/passenger/login";//请求接口地址
        Map<String, Map<String, String>> params = new HashMap();//请求参数体
        Map<String, String> map = new HashMap<>();//请求参数
        map.put("cell", "15895655863");
        map.put("password", "mmmmmmmm");
        Map<String, String> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
               /* JSONObject objec1 = (JSONObject) object.get("data");
                System.out.println(objec1.get("token"));
                token = objec1.get("token").toString();*/
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录
     */
    public static void login() {
        String result = null;
        String url = devUrl+"/ydx/b2b/login";//请求接口地址
        Map<String, Map<String, String>> params = new HashMap();//请求参数体
        Map<String, String> map = new HashMap<>();//请求参数
        map.put("username", "77777777777");
        map.put("password", "11111111");
        Map<String, String> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                JSONObject objec1 = (JSONObject) object.get("data");
                System.out.println(objec1);
                System.out.println(objec1.get("token"));
                token = objec1.get("token").toString();
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //https://restapi.amap.com/v3/distance?origins=116.481028,39.989643&destination=114.465302,40.004717&output=xml&key=f3dca6feeef3b0faae92623638d114e7

    /**
     * 索价
     */
    public static void getPrice() {
        String result = null;
        String url = devUrl+"/ydx/b2b/getPrice";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数
        map.put("departTime", 1956321586325l);
        map.put("airportId", 2);
        map.put("type", 2);
        map.put("flight_no", "C");
        map.put("location", "116.481028,39.989643");
        //map.put("distance", "700");
        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("token", token);
        promap.put("ver", "1.0");
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg")+":"+object.get("data"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建订单
     */
    public static void createOrder() {
        String result = null;
        String url = devUrl+"/ydx/b2b/createOrder";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数
        map.put("orderCreatorId", "1143");
        map.put("partnerOrderId", "1235542515");
        map.put("orderPrice", "80");
        map.put("vehicleTypeId", "4");
        map.put("type", "2");
        map.put("flight_no", "CZ9263");
        map.put("flightDate", "20200123");
        map.put("distance", "1324");
        map.put("userName", "测试员1");
        map.put("cellPhone", "18867310921");
        map.put("departureTime",1577971120000l);

        //子级参数1
        Map<String, String> departureMap = new HashMap<>();
        departureMap.put("address", "虹桥机场");
        departureMap.put("lng", "121365917");
        departureMap.put("lat", "31231813");
        map.put("departure", departureMap);
        //子级参数2
        Map<String, String> destMap = new HashMap<>();
        destMap.put("address", "中山公园(地铁站)");
        destMap.put("lng", "121415741");
        destMap.put("lat", "31231814");
        map.put("destination", destMap);

        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg")+":"+object.get("data"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消订单
     */
    public static void cancelOrder() {
        String result = null;
        String url = devUrl+"/ydx/b2b/cancelOrder";//请求接口地址
        Map<String, Map<String, String>> params = new HashMap();//请求参数体
        Map<String, String> map = new HashMap<>();//请求参数
        map.put("orderNo", "YDX2018091117234002530000");
        Map<String, String> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 通知订单已经支付
     */
    public static void payOrderNotify() {
        String result = null;
        String url = devUrl+"/ydx/b2b/payOrderNotify";//请求接口地址
        Map<String, Map<String, String>> params = new HashMap();//请求参数体
        Map<String, String> map = new HashMap<>();//请求参数
        map.put("orderNo", "YDX2018100915562209250002");
        Map<String, String> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询订单详情
     */
    public static void getOrderInfo() {
        String result = null;
        String url =devUrl+ "/ydx/b2b/getOrderInfo";//请求接口地址
        Map<String, Map<String, String>> params = new HashMap();//请求参数体
        Map<String, String> map = new HashMap<>();//请求参数
        map.put("orderNo", "YDX2018100910384507610003");
        Map<String, String> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object);
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询订单列表
     */
    public static void getOrderList() {
        String result = null;
        String url = devUrl+"/ydx/b2b/getOrderList";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数
        map.put("orderCreatorId", "1143");
        map.put("statusType", "0");
        //参数子级1
        Map<String, String> pageMap = new HashMap<>();
        pageMap.put("page", "1");
        pageMap.put("pageSize", "10");
        map.put("pageParam", pageMap);

        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建发票
     */
    public static void createReceipt() {
        String result = null;
        String url = localUrl+"/ydx/b2b/createReceipt";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数
        map.put("address", "586");
        map.put("amount", "100");
        map.put("cell", "18867310928");
        map.put("description", "张伟达");
        map.put("email", "4564654@qq.com");
        map.put("name", "阿萨德");
        //需要开具的订单类型
        String[] orderNos = new String[1];
        orderNos[0] = "YDX2018100910384507610003";
        map.put("orderNos", orderNos);
        map.put("receiptType", "0");
        map.put("title", "586");
        map.put("identityNo", "123564897523");

        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object);
            } else {
                System.out.println(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取合作方（我方）联系消息
     */
    public static void getPartnerInfo() {
        String result = null;
        String url = devUrl+"/ydx/b2b/getPartnerInfo";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数

        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
                System.out.println(object.get("data"));
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 售后服务接口
     */
    public static void afterSales() {
        String result = null;
        String url = devUrl+"/ydx/b2b/afterSales";//请求接口地址
        Map<String, Map<String, Object>> params = new HashMap();//请求参数体
        Map<String, Object> map = new HashMap<>();//请求参数
        map.put("orderCreatorId", "1143");
        map.put("orderNo", "YDX2018061315160300760011");
        map.put("cell", "18867310921");
        map.put("description", "");

        map.put("email", "4564654@qq.com");
        map.put("name", "许先生");
        map.put("title", "阿萨德公司");

        Map<String, Object> promap = new HashMap<>();//协议说明
        promap.put("appkey", "");
        promap.put("channel", "XIAOMI");
        promap.put("ver", "1.0");
        promap.put("token", token);
        //拼接参数体
        params.put("params", map);
        params.put("protocol", promap);

        try {
            result = net(url, params, "POST");
            JSONObject object = JSONObject.fromObject(result);
            if (object.getInt("code") == 0) {
            } else {
                System.out.println(object.get("code") + ":" + object.get("msg") + object.get("data"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * @param strUrl 请求地址
     * @param params 请求参数
     * @param method 请求方法
     * @return 网络请求字符串
     * @throws Exception
     */
    public static String net(String strUrl, Map params, String method) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            StringBuffer sb = new StringBuffer();
            if (method == null || method.equals("GET")) {
                strUrl = strUrl + "?" + urlencode(params);
            }
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            if (method == null || method.equals("GET")) {
                conn.setRequestMethod("GET");
            } else {
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
            }
            conn.setRequestProperty("User-agent", userAgent);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setUseCaches(false);
            conn.setConnectTimeout(DEF_CONN_TIMEOUT);
            conn.setReadTimeout(DEF_READ_TIMEOUT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            if (params != null && method.equals("POST")) {
                try {
                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                    out.write(urlencode(params).getBytes("utf-8"));
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

    //将map型转为请求参数型
    public static String urlencode(Map<String, Object> data) {
        JSONObject jsonObject = JSONObject.fromObject(data);
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }


}
